<?php

namespace App\Console\Commands;

use App\Models\Report;
use App\PayAllFee;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Models\ParkInfo;
use App\Models\ParkRecord;
use App\Models\PayFee;

/**
 * 过夜车辆查询
 * 凌晨3点执行
 * Class OvernightCar
 * @package App\Console\Commands
 */
class PbusTaxiCarFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'pbus:taxi-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '出租车日报表-fix';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $year  = intval(date('Y'));     // this year
        $month = intval(date('n'));     // this month
        $day   = intval(date('j')) - 1; // yesterday
        $this->_mkdata($day, $month, $year);

        // // batch 
        // for ($i = 3; $i <= 11; $i++) { 
        //     $this->_mkdata($i, $year);
        // }
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function _mkdata($day = 0, $month = 0, $year = 0)
    {
        //查询当月数据

        $dayFirst  = date('j', mktime(0, 0, 0, $month, 1, $year));
        $dayLast   = date('j', mktime(0, 0, 0, $month, $day, $year));
        // $dayLast   = date('j', mktime(0, 0, 0, $month+1, 0, $year));
        $monthFull = sprintf('%d-%02d', $year, $month);

        $this->info(sprintf('出租车月报表Start: %s', $monthFull));


        $data = [];
        for ($i = $dayFirst; $i <= $dayLast; $i++) { 
            $timeStart = mktime(0, 0, 0, $month, $i, $year);
            $timeEnd   = mktime(0, 0, 0, $month, $i+1, $year);

            // 车场列表
            $parks = config('custom.taxi_parks_report');

            $row['time']   = date('Y-m-d', $timeStart);

            $row['tnum']   = 0;
            $row['tmoney'] = 0;

            foreach ($parks as $k => $park) {
                $parkRecord = new ParkRecord;
                $parkRecord->setTable(sprintf('t_park_record_%s', $park));

                // 出租车流量
                $num = PayFee::where("park_code", $park)
                                ->where('pay_amount', 500)
                                ->where('pay_time', '>=', $timeStart)
                                ->where('pay_time', '<', $timeEnd)
                                ->count();

                // 出租车流入
                $money = PayFee::where("park_code", $park)
                                ->where('pay_amount', 500)
                                ->where('pay_time', '>=', $timeStart)
                                ->where('pay_time', '<', $timeEnd)
                                ->sum('pay_amount');

                $k = ($k==0 ? '' : $k+1);
                $row['num'. $k]   = $num;
                $row['money'. $k] = re2float($money/100);
                $row['tnum'] += $num;
                $row['tmoney'] += $money;
            }

            $row['tmoney'] = re2float($row['tmoney']/100);

            $data[] = $row;

            $this->info(sprintf('出租车日报表 %s', $row['time']));
        }

        $report = Report::where('report_date', $monthFull)
                        ->where('type', 'pbus_taxi_day_report')->first();
        if (!$report) {
            $report = new Report;
            $report->report_date = $monthFull;
            $report->type = 'pbus_taxi_day_report';
            $report->value = [];
        }

        $report->value = $data;
        $report->report_date = $monthFull;
        $report->type = 'pbus_taxi_day_report';

        $report->save();
        $this->info(sprintf('出租车月报表End: %s', $monthFull));
    }
}
